package com.yozinga.processor;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.Gson;
import com.yozinga.entity.Category;
import com.yozinga.entity.Offers;

public class CategoryProcessor {

	String mCatgoryId;
	String mCatgoryName;
	String mSubCategoryId;
	String msubCategoryName;

	public CategoryProcessor(String catgoryId/*,String catgoryName*//*,String subCategoryId*//*,String subCategoryName*/) {
		mCatgoryId = catgoryId;
		//mCatgoryName = catgoryName;
		//mSubCategoryId = subCategoryId;
		//msubCategoryName = subCategoryName;
	}
	ArrayList<Category> mCategoryList;
	public ArrayList<Category> getList(String response){

		if(response!=null){
			mCategoryList = new ArrayList<Category>();
			try{

				JSONArray jArray = new JSONArray(response);
				int count = jArray.length();
				Gson _gson = new Gson();
				HashMap tempHashMap = new HashMap();
				Log.i("Today","Merchant List Count is:"+count);
				for(int i=0;i<count;i++){
					Category bean = _gson.fromJson(jArray.getString(i), Category.class);
					if(!jArray.isNull(i))
					{

						JSONObject jsonObject = jArray.getJSONObject(i);

						//CategoryList Fields
						bean.setMerchantId(jsonObject.getString("Id"));
						bean.setBusinessName(jsonObject.getString("BuisnessName"));
						bean.setMerchantTagLine(jsonObject.getString("MerchantTagLine"));

						JSONArray merchantAddressDetails = jArray.getJSONObject(i).getJSONArray("MerchantAddressDetails");
						JSONObject merchantAddressJsonObject = merchantAddressDetails.getJSONObject(0);
						String Latitude = merchantAddressJsonObject.getString("Latitude");
						String Longitude = merchantAddressJsonObject.getString("Longitude");
						bean.setLatitude(Latitude);
						bean.setLongitude(Longitude);


						//MerchantAddress Details Array
						String Country = merchantAddressJsonObject.getString("Country");
						String City = merchantAddressJsonObject.getString("City");
						String State = merchantAddressJsonObject.getString("State");
						String streetAddress = merchantAddressJsonObject.getString("StreetAddress");
						//bean.setInfo(jsonObject.getString("merchatninfo"));
						bean.setStreetAddress(streetAddress);
						bean.setState(State);
						bean.setCity(City);
						bean.setCountry(Country);
						bean.setMerchantFullAddress(streetAddress+", "+State+", "+City+", "+Country);
						bean.setPhoneNumber(jsonObject.getString("TelephoneNumber"));
						bean.setVideoUrl(jsonObject.getString("VideoLink"));
						bean.setFaceBookUrl(jsonObject.getString("Facebook"));
						bean.setTwitterUrl(jsonObject.getString("Twitter"));
						bean.setPaypalEmail(jsonObject.getString("PaypalEmail"));
						//bean.setDrippleUrl(jsonObject.getString("driple url"));




						//OfferDetails
						JSONArray offerDetails = jArray.getJSONObject(i).getJSONArray("OfferDetails");
						int offerDetailsCount = offerDetails.length();
						Gson _gson1 = new Gson();
						ArrayList<Offers> offerDetailsList = new ArrayList<Offers>();
						Log.i("Today","offerDetailsCount Count is:"+offerDetailsCount);
						for (int j = 0; j < offerDetailsCount; j++) {

							JSONObject offerJsonObject = offerDetails.getJSONObject(j);
							String categoryId = offerJsonObject.getString("CategoryId");
							String subCategoryId = offerJsonObject.getString("SubCategoryId");

							Log.i("Today","Category ID:"+categoryId);
							Log.i("Today","mCatgoryId is ID:"+mCatgoryId);
							if((mCatgoryId!=null && mCatgoryId.equals(categoryId))|| mCatgoryId!=null && mCatgoryId.equals(subCategoryId)){

								Log.i("Today","Both ids are equal");
								Offers offerBean = _gson1.fromJson(offerDetails.getString(j), Offers.class);

								//For OfferList Fields
								offerBean.setOfferId(offerJsonObject.getString("Id"));
								offerBean.setDealName(offerJsonObject.getString("Headline"));
								offerBean.setDealDesc(offerJsonObject.getString("Description"));
								offerBean.setThumbnailImage(offerJsonObject.getString("ThumbnailImage"));

								//For Deal Page Fields
								offerBean.setOfferTagLine(offerJsonObject.getString("OfferTagLine"));
								offerBean.setOfferStart(offerJsonObject.getString("OfferStart"));
								offerBean.setOfferEnd(offerJsonObject.getString("OfferEnd"));
								offerBean.setOfferLimitations(offerJsonObject.getString("OfferLimitations"));
								offerBean.setImageLogoUrl(offerJsonObject.getString("FullImage"));
								offerBean.setRating(offerJsonObject.getString("Reviews"));
								offerBean.setOfferPrice(offerJsonObject.getString("OfferPrice"));
								offerBean.setOfferPrice(offerJsonObject.getString("DiscountedPrice"));
								offerDetailsList.add(offerBean);
							}else{
								Log.i("Today","Both ids are not equal");
							}

						}
						Log.i("Today","Offer List Size is:"+offerDetailsList.size());
						bean.setOfferDetails(offerDetailsList);
						bean.setOffers(offerDetailsList.size()+"");
						if(Integer.parseInt(bean.getOffers())>0)
						mCategoryList.add(bean);
					}
				}
				for(Category i : mCategoryList){
					Log.i("NCS","Offers  :"+i.getOffers());
				}

			}catch (Exception e) {
				Log.i("NCS","Exception   :"+e.getMessage());
			}
		}
		return mCategoryList;
	}
}
